﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//buddy
using Zeta;
using Zeta.Common;
using Zeta.CommonBot;
using Zeta.Common.Plugins;
using Zeta.Internals;
using Zeta.Internals.Actors;
using Zeta.Internals.Service;

namespace MyBuddy.Local
{
    public class BuddyAutoFollower : IPlugin
    {
        DateTime LastPostionCheck { get; set; }
        bool enabled = false;

        public string Author
        {
            get { return "xsol"; }
        }

        public string Description
        {
            get { return "Follows the leader."; }
        }

        public System.Windows.Window DisplayWindow
        {
            get { return null; }
        }

        public string Name
        {
            get { return "BuddyAutoFollower"; }
        }

        public void OnDisabled()
        {
            enabled = false;
        }

        public void OnEnabled()
        {
            enabled = true;
        }

        public void OnInitialize()
        {

        }

        ACD leader = null;
        public void OnPulse()
        {
            if (enabled == false) return;

            if (DateTime.Now.Subtract(LastPostionCheck).TotalMilliseconds > 2250)
            {
                try { Zeta.CommonBot.LootTargeting.Instance.Pulse(); }
                catch (Exception exLooter) { Logging.Write(LogLevel.Verbose, "BuddyAutoFollower: " + exLooter.Message); }

                LastPostionCheck = DateTime.Now;
                try
                {
                    //find someone to follow
                    IEnumerator<Actor> ie = ZetaDia.Actors.ACDList.GetEnumerator();
                    while (ie.MoveNext())
                    {
                        if (null != ie.Current)
                        {
                            Actor act = (Actor)ie.Current;
                            ACD acd = (ACD)act;

                            if (acd.Name != ZetaDia.Me.Name && acd.ActorType == Zeta.Internals.SNO.ActorType.Player)
                            {
                                if (acd.Position.Distance(ZetaDia.Me.Position) < 60)
                                {
                                    leader = acd;//track this global but don't depend on it
                                    break;
                                }
                            }
                        }
                    }
                    //p => !p.IsAfk && !p.Position.Distance(ZetaDia.Me.Position) < 60 && !p.Name == ZetaDia.Me.Name);

                    if (null != leader)
                    {
                        try
                        {
                            Random r = new Random();
                            Vector3 vary = new Vector3(r.Next(-1, 1), r.Next(-1, 1), r.Next(-1, 1));
                            Vector3 moveTo = leader.Position;
                            moveTo += vary;
                            ZetaDia.Me.UsePower(SNOPower.Walk, moveTo, ZetaDia.Me.WorldDynamicId, -1);
                        }
                        catch (Exception leaderEx)
                        {
                            Logging.Write(LogLevel.Verbose, "BuddyAutFollow: " + leaderEx.Message);
                        }
                    }

                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Verbose, "BuddyAutFollow: " + ex.Message);
                }

                try { Zeta.CommonBot.CombatTargeting.Instance.Pulse(); }
                catch (Exception exFight) { Logging.Write(LogLevel.Verbose, "BuddyAutoFollower: " + exFight.Message); }
            }
        }

        public void OnShutdown()
        {

        }

        public Version Version
        {
            get { return new Version(0, 0, 1); }
        }

        public bool Equals(IPlugin other)
        {
            return (this.Name == other.Name && this.Version == other.Version) ? true : false;
        }
    }
}